//
//  PickNumSportViewHHSelectController.m
//  PlayBall
//
//  Created by Jdd‘s macbook on 13-10-31.
//
//

#import "JddPickNumSportViewHHSelectController.h"
#import "JddViewControllerManager.h"
#import "JddHH_TableView.h"
@interface JddPickNumSportViewHHSelectController ()

@end

@implementation JddPickNumSportViewHHSelectController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"JCZQ-hunhewanfa"];

}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"JCZQ-hunhewanfa"];
}
//- (void)backView
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"赛事方案";
    //初始化scrollView
    [self initScrollView];
    //初始化数据
    [self initData];
    //初始化按钮状态
    [self initBtn];
    
    spfArr = [NSMutableArray arrayWithObject:@"胜平负:"];
    rqspfArr = [NSMutableArray arrayWithObject:@"让球胜平负:"];
    zjcArr = [NSMutableArray arrayWithObject:@"总进球:"];
    bqcArr = [NSMutableArray arrayWithObject:@"半全场:"];
    bfArr = [NSMutableArray arrayWithObject:@"比分:"];
    
    resultStr = [NSMutableString string];
    resultArray = [NSMutableArray arrayWithObjects:spfArr, rqspfArr, zjcArr, bqcArr, bfArr, nil];
}

-(void)initScrollView
{
    CGRect frame = scroll.frame;
    frame.size.height = SCREEN_HEIGHT-64-55-40;
    scroll.frame = frame;
    [scroll setContentSize:CGSizeMake(320, 480)];
}

-(void)initData
{
    //初始化标题栏
    NSString *wk = [self.arr objectAtIndex:0];
    NSString *num = [self.MatchCell objectForKey:@"MID"];
    num = [NSString stringWithFormat:@"%@",[num substringFromIndex:6]];
    NSString *HTeam = [self.MatchCell objectForKey:@"HTeam"];
    NSString *VTeam = [self.MatchCell objectForKey:@"VTeam"];
    
    NSString *titleStr = [NSString stringWithFormat:@"%@%@  %@VS%@", wk, num, HTeam, VTeam];
    titleLbl.text = titleStr;
    
    //胜平负
    NSString *spspfStr = [self.MatchCell objectForKey:@"SpSPF"];
    if (spspfStr.length > 0) {
        NSArray *SPSPF = [spspfStr componentsSeparatedByString:@"|"];
        spfLbl1.text = [SPSPF objectAtIndex:0];
        spfLbl2.text = [SPSPF objectAtIndex:1];
        spfLbl3.text = [SPSPF objectAtIndex:2];
    }else{
        for(UIView *u in scroll.subviews){
            if ([u isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)u;
                if (btn.tag == 1) {
                    btn.enabled = NO;
                }
            }
        }
    }
    
    //让球胜平负
    NSArray *SpRQSPF = [[self.MatchCell objectForKey:@"SpRQSPF"] componentsSeparatedByString:@"|"];
    rqspfLbl1.text = [SpRQSPF objectAtIndex:0];
    rqspfLbl2.text = [SpRQSPF objectAtIndex:1];
    rqspfLbl3.text = [SpRQSPF objectAtIndex:2];
    //总进球
    NSArray *SpZJQ= [[self.MatchCell objectForKey:@"SpZJQ"] componentsSeparatedByString:@"|"];
    zjqLbl1.text = [SpZJQ objectAtIndex:0];
    zjqLbl2.text = [SpZJQ objectAtIndex:1];
    zjqLbl3.text = [SpZJQ objectAtIndex:2];
    zjqLbl4.text = [SpZJQ objectAtIndex:3];
    zjqLbl5.text = [SpZJQ objectAtIndex:4];
    zjqLbl6.text = [SpZJQ objectAtIndex:5];
    zjqLbl7.text = [SpZJQ objectAtIndex:6];
    zjqLbl8.text = [SpZJQ objectAtIndex:7];
    //半全场
    NSArray *SpBQC = [[self.MatchCell objectForKey:@"SpBQC"] componentsSeparatedByString:@"|"];
    bqcLbl1.text = [SpBQC objectAtIndex:0];
    bqcLbl2.text = [SpBQC objectAtIndex:1];
    bqcLbl3.text = [SpBQC objectAtIndex:2];
    bqcLbl4.text = [SpBQC objectAtIndex:3];
    bqcLbl5.text = [SpBQC objectAtIndex:4];
    bqcLbl6.text = [SpBQC objectAtIndex:5];
    bqcLbl7.text = [SpBQC objectAtIndex:6];
    bqcLbl8.text = [SpBQC objectAtIndex:7];
    bqcLbl9.text = [SpBQC objectAtIndex:8];
    //比分
    NSArray *spcbf = [[self.MatchCell objectForKey:@"SpCBF"] componentsSeparatedByString:@"|"];
    //主
    zLbl1.text = [spcbf objectAtIndex:0];
    zLbl2.text = [spcbf objectAtIndex:1];
    zLbl3.text = [spcbf objectAtIndex:2];
    zLbl4.text = [spcbf objectAtIndex:3];
    zLbl5.text = [spcbf objectAtIndex:4];
    zLbl6.text = [spcbf objectAtIndex:5];
    zLbl7.text = [spcbf objectAtIndex:6];
    zLbl8.text = [spcbf objectAtIndex:7];
    zLbl9.text = [spcbf objectAtIndex:8];
    zLbl10.text = [spcbf objectAtIndex:9];
    zLbl11.text = [spcbf objectAtIndex:10];
    zLbl12.text = [spcbf objectAtIndex:11];
    zLbl13.text = [spcbf objectAtIndex:12];
    //平
    pLbl1.text = [spcbf objectAtIndex:13];
    pLbl2.text = [spcbf objectAtIndex:14];
    pLbl3.text = [spcbf objectAtIndex:15];
    pLbl4.text = [spcbf objectAtIndex:16];
    pLbl5.text = [spcbf objectAtIndex:17];
    //客
    kLbl1.text = [spcbf objectAtIndex:18];
    kLbl2.text = [spcbf objectAtIndex:19];
    kLbl3.text = [spcbf objectAtIndex:20];
    kLbl4.text = [spcbf objectAtIndex:21];
    kLbl5.text = [spcbf objectAtIndex:22];
    kLbl6.text = [spcbf objectAtIndex:23];
    kLbl7.text = [spcbf objectAtIndex:24];
    kLbl8.text = [spcbf objectAtIndex:25];
    kLbl9.text = [spcbf objectAtIndex:26];
    kLbl10.text = [spcbf objectAtIndex:27];
    kLbl11.text = [spcbf objectAtIndex:28];
    kLbl12.text = [spcbf objectAtIndex:29];
    kLbl13.text = [spcbf objectAtIndex:30];
}

-(void)initBtn
{
    for(UIView *u in scroll.subviews){
        if ([u isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)u;
            [btn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
            [btn setBackgroundImage:LOADIMAGE(@"sport_select") forState:UIControlStateSelected];
            //设置边框
            [btn.layer setMasksToBounds:YES];
            [btn.layer setBorderWidth:0.5]; //边框宽度
//            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//            CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0.9294f, 0.5686f, 0.1294f, 1.0f });
            [btn.layer setBorderColor:BORDER_COLOR];//边框颜色
        }
        if (self.arr.count > 2) {
        NSMutableArray *resultArrs = [self.arr objectAtIndex:2];
        if (resultArrs.count > 0) {            
        NSArray *spfA = [resultArrs objectAtIndex:0];
        NSArray *rqspfA = [resultArrs objectAtIndex:1];
        NSArray *zjcA = [resultArrs objectAtIndex:2];
        NSArray *bqcA = [resultArrs objectAtIndex:3];
        NSArray *bfA = [resultArrs objectAtIndex:4];
            
        if ([u isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)u;
            switch (btn.tag) {
                case 1:
                    [self setBtnSelectedWithArray:spfA AndBtn:btn];
                    break;
                case 2:
                    [self setBtnSelectedWithArray:rqspfA AndBtn:btn];
                    break;
                case 3:
                    [self setBtnSelectedWithArray:zjcA AndBtn:btn];
                    break;
                case 4:
                    [self setBtnSelectedWithArray:bqcA AndBtn:btn];
                    break;
                case 5:
                    [self setBtnSelectedWithArray:bfA AndBtn:btn];
                    break;
                default:
                    break;
            }
            }
        }
    }
    }
}

-(void)setBtnSelectedWithArray:(NSArray *)arr AndBtn:(UIButton *)btn
{
    if (arr.count > 1) {
        for(NSString *s in arr){
            NSString *trimmedString = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            if ([btn.titleLabel.text isEqualToString:trimmedString]) {
                [btn setSelected:YES];
            }
        }
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [self popWindow];
    }
}

-(IBAction)backAction:(id)sender
{
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                         message:@"退出后您的选项将不会保存，确定继续？"
                                                        delegate:self
                                               cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];    [alertView show];
}

-(void)popWindow
{
    JddHH_TableView *h = (JddHH_TableView *)self.fromController;
    NSIndexPath *indexPath = (NSIndexPath *)[self.arr objectAtIndex:1];
    [h sportTeamButtonClicked:indexPath AndResultStr:resultStr AndResultArr:resultArray];
     [[JddViewControllerManager sharedInstance] popToBackViewController];
}

-(IBAction)selectAction:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    btn.selected = !btn.selected;
}

-(IBAction)okAction:(id)sender
{
    for(UIView *u in scroll.subviews){
        if ([u isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)u;
            int btnTag = btn.tag;
            if (btn.selected) {
                NSString *title = [NSString stringWithFormat:@"%@ ", btn.titleLabel.text];
                switch (btnTag) {
                    case 1:
                        [spfArr addObject:title];
                        break;
                    case 2:
                        [rqspfArr addObject:title];
                        break;
                    case 3:
                        [zjcArr addObject:title];
                        break;
                    case 4:
                        [bqcArr addObject:title];
                        break;
                    case 5:
                        [bfArr addObject:title];
                        break;
                    default:
                        break;
                }
            }
        }
    } 
    if (spfArr.count > 1) {
        NSString *spfStr = [spfArr componentsJoinedByString:@""];
        [resultStr appendString:spfStr];
    }
    if (rqspfArr.count > 1) {
        NSString *rqspStr = [rqspfArr componentsJoinedByString:@""];
        [resultStr appendString:rqspStr];
    }
    if (zjcArr.count > 1) {
        NSString *zjcStr = [zjcArr componentsJoinedByString:@""];
        [resultStr appendString:zjcStr];
    }
    if (bqcArr.count > 1) {
        NSString *bqcStr = [bqcArr componentsJoinedByString:@""];
        [resultStr appendString:bqcStr];
    }
    if (bfArr.count > 1) {
        NSString *bfStr = [bfArr componentsJoinedByString:@""];
        [resultStr appendString:bfStr];
    }
    [self popWindow];
}


@end
